package com.ai.ubssapp.controller;

import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.MapUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.ai.ubssapp.bean.RespEncrypt;
import com.ai.ubssapp.bean.RespInfo;
import com.ai.ubssapp.constant.Constants;
import com.ai.ubssapp.service.VersionService;
import com.ai.ubssapp.util.ParamTool;

/**
 * app自动版本更新接口
 * @author xmh
 *
 */
@RestController
@RequestMapping("/version")
public class VersionController{
	
	@Resource(name = "versionService")
	private VersionService versionService;
	
	/**
	 * 根据android还是ios系统查询当前最新的app版本号和下载地址
	 * @param inParam
	 * @return
	 */
	@RequestMapping(value = "/queryLatestVersion", method = RequestMethod.POST)
	public RespEncrypt queryLatestVersion(@RequestBody Map<String, Object> inParam){
		RespEncrypt respEncrypt = new RespEncrypt();
		
		RespInfo respInfo = new RespInfo();
		//20160413 ximh 修改，app请求参数是加密过的，这边需要解密后再执行查询
		//IData<String, Object> row = versionService.queryLatestVersion(inParam);
		Map<String, Object> newParam = ParamTool.transReqParam(inParam);
		if(newParam.containsKey("os")){
			Map<String, Object> row = versionService.queryLatestVersion(newParam);
			if(MapUtils.isNotEmpty(row)){
				respInfo.setRespCode(Constants.RESP_CODE_SUCCESS);
				respInfo.setRespDesc("获取最新版本成功");
				respInfo.setData(row);
			}
			else{
				respInfo.setRespCode(Constants.RESP_CODE_FAILURE);
				respInfo.setRespDesc("没有查询到最新版本号");
			}
		}
		else{
			respInfo.setRespCode(Constants.RESP_CODE_FAILURE);
			respInfo.setRespDesc("请求中必须有os参数");
		}

		//原本返回结果是明文的，ubssapp需要加密后再返回给app
		String newRespInfo = ParamTool.transRespInfo(respInfo); //加密后的密文响应
		respEncrypt.setRespInfo(newRespInfo); //包装下
		return respEncrypt;
	}

}
